package eclipse.weibo.modle;

import java.io.File;
import java.net.MalformedURLException;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import weibo4j.User;
import weibo4j.util.DebugPrn;
import eclipse.weibo.Activator;
import eclipse.weibo.util.PatternUtil;
import eclipse.weibo.util.TimeDateUtil;

public class UserLabelProvider extends LabelProvider implements ITableLabelProvider {
    private static DebugPrn dp = new DebugPrn(UserLabelProvider.class.getName());
    private static Image waitImg = getWaitImg();

    private static Image getWaitImg() {
        ImageDescriptor imgd = Activator.getImageDescriptor("icons/ajax-loading.gif");
        if (imgd == null) {
            File file = new File("D:/eclipse/workspace/EclipseWeibo/icons/ajax-loading.gif");
            try {
                imgd = ImageDescriptor.createFromURL(file.toURI().toURL());
            } catch (MalformedURLException ex) {
                dp.error(ex, ex);
            }
        }
        Image img = imgd.createImage();
        return new Image(null, img.getImageData().scaledTo(50, 50));
    }

    public Image getColumnImage(Object element, int columnIndex) {
        if ((columnIndex == 0) && (Activator.isShowImage())) {
            return waitImg;
        }
        return null;
    }

    public String getColumnText(Object element, int columnIndex) {
        if (!(element instanceof User)) {
            return null;
        }
        User c = (User) element;
        if (columnIndex == 0) {
            return null;
        }
        if (columnIndex == 1) {
            return c.getScreenName();
        }
        if (columnIndex == 2) {
            String str = c.getStatusText();
            return str;
        }
        if (columnIndex == 3) {
            if (c.getStatusCreatedAt() != null) {
                return TimeDateUtil.getWeiboTime(c.getStatusCreatedAt());
            }
            return "";
        }
        if (columnIndex == 4) {
            if (c.getStatusSource() != null) {
                return PatternUtil.getA(c.getStatusSource());
            }
            return "";
        }
        if (columnIndex == 5) {
            return String.valueOf(c.getFriendsCount());
        }
        if (columnIndex == 6) {
            return String.valueOf(c.getFollowersCount());
        }
        return null;
    }
}
